#include "stdafx.h"
#include "CppUnitTest.h"
#include "..\src\algorithm.h"
#include <algorithm>
#include <vector>
#include <list>

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace STL_Impl_UnitTest
{

TEST_CLASS(algorithm_unittest)
{
public:

TEST_METHOD(unit_test_find)
{
    std::vector<int> test_vector;
    for(int i = 0; i < 100; ++i)
        test_vector.push_back(i);

    Assert::AreEqual(std::find(test_vector.begin(), test_vector.end(), 80),
        stl::find(test_vector.begin(), test_vector.end(), 80));

    Assert::AreEqual(std::find(test_vector.begin(), test_vector.end(), 800),
        stl::find(test_vector.begin(), test_vector.end(), 800));
}

TEST_METHOD(unit_test_find_if)
{
    std::list<int> test_list;
    for(int i = 0; i < 100; ++i)
        test_list.push_back(i);

    Assert::AreEqual(std::find_if(test_list.begin(), test_list.end(), 
        [](int item){return item == 80;}),
        stl::find_if(test_list.begin(), test_list.end(), 
        [](int item){return item == 80;}));

    Assert::AreNotEqual(std::find_if(test_list.begin(), test_list.end(), 
        [](int item){return item == 1080;}),
        stl::find_if(test_list.begin(), test_list.end(), 
        [](int item){return item == 80;}));
}

TEST_METHOD(unit_test_find_end)
{
    int myints[] = {1,2,3,4,5,1,2,3,4,5};
    std::vector<int> myvector (myints,myints+10);
                        
    int match1[] = {1,2,3};
    std::vector<int> match(match1, match1+3);

    auto stl_ret = stl::find_end(myvector.begin(), myvector.end(),
        match.begin(), match.end());

    auto std_ret = std::find_end(myvector.begin(), myvector.end(),
                    match.begin(), match.end());

    Assert::AreEqual(stl_ret, std_ret);

    auto stl_result = stl::find_end(myvector.begin(), myvector.end(),
        match.begin(), match.end(), [](int a, int b){return a == b;});

    auto std_result = std::find_end(myvector.begin(), myvector.end(),
        match.begin(), match.end(), [](int a, int b){return a == b;});

    Assert::AreEqual(stl_result, std_result);
}

TEST_METHOD(unit_test_find_first_of)
{
    int myints[] = {1,2,3,4,5,1,2,3,4,5};
    std::vector<int> myvector (myints,myints+10);

    int match1[] = {8,9,10};
    std::vector<int> match(match1, match1+3);

    auto stl_ret = stl::find_first_of(myvector.begin(), myvector.end(),
        match.begin(), match.end());

    auto std_ret = std::find_first_of(myvector.begin(), myvector.end(),
        match.begin(), match.end());

    Assert::AreEqual(stl_ret, std_ret);

    auto stl_result = stl::find_first_of(myvector.begin(), myvector.end(),
        match.begin(), match.end(), [](int a, int b){return a == b;});

    auto std_result = std::find_first_of(myvector.begin(), myvector.end(),
        match.begin(), match.end(), [](int a, int b){return a == b;});

    Assert::AreEqual(stl_result, std_result);
}

TEST_METHOD(unit_test_for_each)
{
    int myints[] = {1,2,3,4,5,1,2,3,4,5};
    std::vector<int> myvector (myints,myints+10);

    int count = 0;
    stl::for_each(myvector.begin(), myvector.end(), [&](int item){++count;});

    Assert::AreEqual(count, 10);
}

}; //End of TEST_CLASS

}  // End of namespace STL_Impl_UnitTest